package com.epro.dx.widget;

import android.animation.ObjectAnimator;
import android.animation.ValueAnimator;
import android.app.Activity;
import android.content.Context;
import android.graphics.Point;
import android.util.AttributeSet;
import android.view.MotionEvent;
import com.epro.dx.util.ActivityUtil;
import com.romainpiel.shimmer.ShimmerTextView;

/**
 * 解锁自定义控件，当滑动到一定点距离，即解锁
 * @author Zenglw
 * @version 1.0
 * @date 2017-6-29
 */

public class HoriScrollShimmerTV extends ShimmerTextView {
    //按下去的x位置
    private int oldX;
    //最原始的位移
    private int oldTransX;
    //屏幕的宽度
    private int screenWidth;
    //自身的宽度
    private int selfWidth;
    //边界值
    private int MAXIMUN;
    private OnUnlockListener mListener;
    private OnScrollListener mScrollListener;
    private int dx;
    //自动滑回到默认的位置所用到的时间
    private int autoScrollDefaultPositionDuration;

    public HoriScrollShimmerTV(Context context) {
        this(context, null);
    }

    public HoriScrollShimmerTV(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public HoriScrollShimmerTV(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        Point screenSize = ActivityUtil.getScreenSize((Activity) context);
        screenWidth = screenSize.x;
        autoScrollDefaultPositionDuration = 100;
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        selfWidth = getMeasuredWidth();
//        MAXIMUN = (selfWidth + screenWidth / 2) / 2;
        MAXIMUN = screenWidth / 2;
    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();

    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                oldX = (int) event.getRawX();
                oldTransX = (int) getTranslationX();
                break;

            case MotionEvent.ACTION_MOVE:
                dx = (int) event.getRawX() - oldX;
                if (mScrollListener != null) {
                    mScrollListener.onScroll(dx);
                }
                break;

            case MotionEvent.ACTION_UP:

                if (dx < 0 && Math.abs(dx) >= MAXIMUN) {
                    //当左滑时，距离超过屏幕宽度点一半时，就关闭界面，不做处理
                    if (mScrollListener != null) {
                        mListener.onUnlock();
                    }

                } else if (dx > 0 && Math.abs(dx) >= MAXIMUN) {
                    //当右滑时，距离超过屏幕宽度点一半时，就关闭界面，做其他点处理

                    if (mScrollListener != null) {
                        mListener.onRightScroll();
                    }
                }else {
                    //不属于上面点两种情况，则自动滑回到原来的位置
                    moveDefaultPosition();
                }
                break;

            default:

                break;
        }
        return true;

    }

    /**
     * 动画位移为原来的位置
     */
    private void moveDefaultPosition() {
//        ObjectAnimator.ofFloat(this,"translationX",getTranslationX(),0).setDuration(100).start();
        ObjectAnimator animator = ObjectAnimator.ofFloat(this, "xxx", dx, 0).setDuration(autoScrollDefaultPositionDuration );
        animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                float value = (float) animation.getAnimatedValue();
                if (mScrollListener != null) {
                    mScrollListener.onScroll(value);
                }
            }
        });
        animator.start();

    }

    public interface OnUnlockListener{
        void onUnlock();
        void onRightScroll();
    }

    public void setOnUnlockListener(OnUnlockListener listener) {
        this.mListener = listener;
    }

    public interface OnScrollListener{
        void onScroll(float distance);
    }

    public void setOnScrollListener(OnScrollListener mScrollListener){
        this.mScrollListener = mScrollListener;
    }

    public void setAutoScrollDefaultPositionDuration(int autoScrollDefaultPositionDuration) {
        this.autoScrollDefaultPositionDuration = autoScrollDefaultPositionDuration;
    }
}
